package com.youngking.guns.modular.projectinfo.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.youngking.guns.core.base.controller.BaseController;
import com.youngking.guns.core.util.Common;
import com.youngking.guns.core.util.ToolUtil;
import com.youngking.guns.modular.system.model.Wxuserinfo;
import com.youngking.guns.modular.wxuserinfo.service.IWxuserinfoService;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.youngking.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.youngking.guns.modular.system.model.Projectinfo;
import com.youngking.guns.modular.projectinfo.service.IProjectinfoService;
import org.weixin4j.Weixin;
import org.weixin4j.WeixinException;
import org.weixin4j.model.message.template.TemplateData;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-08-03 13:56:09
 */
@Controller
@RequestMapping("/projectinfo")
@PropertySource("classpath:/aliyun.properties")
public class ProjectinfoController extends BaseController {

    private String PREFIX = "/projectinfo/projectinfo/";

    @Autowired
    private IProjectinfoService projectinfoService;
    @Autowired
    private IWxuserinfoService wxuserinfoService;
    @Value("${accesskeyid}")
    private String accesskeyid;  //验证码发送key
    @Value("${accesskeysecret}")
    private String accesskeysecret;  //验证码秘钥
    /**
     * 跳转到未审核
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "projectinfo.html";
    }

    /**
     * 跳转到待支付
     */
    @RequestMapping("/paylist")
    public String paylistindex() {
        return PREFIX + "projectinfopay.html";
    }

    /**
     * 跳转到未通过
     */
    @RequestMapping("/nopasslist")
    public String nopassindex() {
        return PREFIX + "projectinfonopass.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/projectinfo_add")
    public String projectinfoAdd() {
        return PREFIX + "projectinfo_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/projectinfo_update/{projectinfoId}")
    public String projectinfoUpdate(@PathVariable Integer projectinfoId, Model model) {
        Projectinfo projectinfo = projectinfoService.selectById(projectinfoId);
        String str=  projectinfo.getViewmark().replace("\"","@");
        projectinfo.setViewmark(str);
        model.addAttribute("item",projectinfo);
        LogObjectHolder.me().set(projectinfo);
        return PREFIX + "projectinfo_edit.html";
    }

    /**
     * 跳转到详细
     */
    @RequestMapping("/projectinfo_detail/{projectinfoId}")
    public String projectinfoDetail(@PathVariable Integer projectinfoId, Model model) {
        Projectinfo projectinfo = projectinfoService.selectById(projectinfoId);
        String str=  projectinfo.getRemark().replace("\"","@");
        String str1=  projectinfo.getViewmark().replace("\"","@");
        projectinfo.setRemark(str);
        projectinfo.setViewmark(str1);
        model.addAttribute("item",projectinfo);
        LogObjectHolder.me().set(projectinfo);
        return PREFIX + "projectinfo_detail.html";
    }

    /**
     * 获取未审核列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition,String starttime,String endtime) {
        EntityWrapper ew = new EntityWrapper();
        ew.setEntity(new Projectinfo());
        ew.where("status=0")
                .like(ToolUtil.isNotEmpty(condition),"projectname",condition)
                .orNew(ToolUtil.isNotEmpty(condition),"`address` like {0}", "%" + condition + "%")
                .orNew(ToolUtil.isNotEmpty(condition),"`addressdetail` like {0}", "%" + condition + "%")
                .andNew(ToolUtil.isNotEmpty(starttime),"subtime >= {0}",starttime)
                .andNew(ToolUtil.isNotEmpty(endtime),"subtime <= {0}",endtime)
                .orderBy("id desc");
        return projectinfoService.selectList(ew);
    }

    /**
     * 获取未通过列表
     */
    @RequestMapping(value = "/nopasslistinfo")
    @ResponseBody
    public Object nopasslist(String condition,String starttime,String endtime) {
        EntityWrapper ew = new EntityWrapper();
        ew.setEntity(new Projectinfo());
        ew.where("status=2")
                .like(ToolUtil.isNotEmpty(condition),"projectname",condition)
                .orNew(ToolUtil.isNotEmpty(condition),"`address` like {0}", "%" + condition + "%")
                .orNew(ToolUtil.isNotEmpty(condition),"`addressdetail` like {0}", "%" + condition + "%")
                .andNew(ToolUtil.isNotEmpty(starttime),"subtime >= {0}",starttime)
                .andNew(ToolUtil.isNotEmpty(endtime),"subtime <= {0}",endtime)
                .orderBy("id desc");
        return projectinfoService.selectList(ew);
    }

    /**
     * 获取待支付列表
     */
    @RequestMapping(value = "/paylistinfo")
    @ResponseBody
    public Object paylist(String condition,String starttime,String endtime) {
        EntityWrapper ew = new EntityWrapper();
        ew.setEntity(new Projectinfo());
        ew.where("status=1")
                .like(ToolUtil.isNotEmpty(condition),"projectname",condition)
                .orNew(ToolUtil.isNotEmpty(condition),"`address` like {0}", "%" + condition + "%")
                .orNew(ToolUtil.isNotEmpty(condition),"`addressdetail` like {0}", "%" + condition + "%")
                .andNew(ToolUtil.isNotEmpty(starttime),"subtime >= {0}",starttime)
                .andNew(ToolUtil.isNotEmpty(endtime),"subtime <= {0}",endtime)
                .orderBy("id desc");
        return projectinfoService.selectList(ew);
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer projectinfoId) {
        projectinfoService.deleteById(projectinfoId);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Projectinfo projectinfo) {
        projectinfo.setAudittime(new Date());
        projectinfoService.updateById(projectinfo);
        Projectinfo projectinfo1 = projectinfoService.selectById(projectinfo.getId());
        Weixin weixin = new Weixin();
        Wxuserinfo wxuserinfo = wxuserinfoService.selectOne(new EntityWrapper<Wxuserinfo>().eq("id",projectinfo.getCustomerid())
                .eq("subscribe",1));
        if (projectinfo1.getStatus()==1) {
            List<TemplateData> templateDataList=new ArrayList<>();
            TemplateData templateData= new TemplateData();
            templateData.setColor("");
            templateData.setValue("您提交的项目已审核，请点击查看");
            templateData.setKey("first");
            templateDataList.add(templateData);
            TemplateData templateData1= new TemplateData();
            templateData1.setColor("green");
            templateData1.setValue("通过");
            templateData1.setKey("keyword1");
            templateDataList.add(templateData1);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String audittime =  df.format(new Date());
            TemplateData templateData2= new TemplateData();
            templateData2.setColor("");
            templateData2.setValue(audittime);
            templateData2.setKey("keyword2");
            templateDataList.add(templateData2);
            TemplateData templateData3= new TemplateData();
            templateData3.setColor("");
            templateData3.setValue("项目名称："+projectinfo1.getProjectname());
            templateData3.setKey("remark");
            templateDataList.add(templateData3);
            if(wxuserinfo!=null){
                try {
                    weixin.message().sendTemplateMessage(wxuserinfo.getOpenid(),"2qdoNTx7U5oXras8wAbgoJPFfSCH_N5VQ_k86HsAxBY",templateDataList,"http://www.ynyxgcxx.com/inquier?id="+projectinfo1.getId()+"&wxuserinfoid="+wxuserinfo.getId());
                } catch (WeixinException e) {
                    e.printStackTrace();
                }
            }
        }
        if (projectinfo1.getStatus() == 2) {
            TemplateData templateData= new TemplateData();
            templateData.setColor("");
            templateData.setValue("您提交的项目已审核，请点击查看");
            templateData.setKey("first");
            List<TemplateData> templateDataList=new ArrayList<>();
            templateDataList.add(templateData);
            TemplateData templateData1= new TemplateData();
            templateData1.setColor("red");
            templateData1.setValue("未通过");
            templateData1.setKey("keyword1");
            templateDataList.add(templateData1);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String audittime =  df.format(new Date());
            TemplateData templateData2= new TemplateData();
            templateData2.setColor("");
            templateData2.setValue(audittime);
            templateData2.setKey("keyword2");
            templateDataList.add(templateData2);
            TemplateData templateData3= new TemplateData();
            templateData3.setColor("");
            templateData3.setValue("项目名称："+projectinfo1.getProjectname());
            templateData3.setKey("remark");
            templateDataList.add(templateData3);
            if(wxuserinfo!=null){
                try {
                    weixin.message().sendTemplateMessage(wxuserinfo.getOpenid(),"2qdoNTx7U5oXras8wAbgoJPFfSCH_N5VQ_k86HsAxBY",templateDataList,"http://www.ynyxgcxx.com/inquier?id="+projectinfo1.getId()+"&wxuserinfoid="+wxuserinfo.getId());
                } catch (WeixinException e) {
                    e.printStackTrace();
                }
            }
        }
        return SUCCESS_TIP;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{projectinfoId}")
    @ResponseBody
    public Object detail(@PathVariable("projectinfoId") Integer projectinfoId) {
        return projectinfoService.selectById(projectinfoId);
    }
}
